package com.wsj.dao.hibernate;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.wsj.dao.IHomeSiteAdBannerDescriptDao;
import com.wsj.util.page.PageResultSet;
import com.wsj.util.page.PerfectPageResultSet;
import com.wsj.util.tool.StringUtils;
import com.wsj.util.tool.Tool;
import com.wsj.web.vo.homeSite.HomeSiteAdBannerDescription;

public class HomeSiteAdBannerDescriptDaoImpl extends HibernateDaoSupport implements IHomeSiteAdBannerDescriptDao {

	public void add(HomeSiteAdBannerDescription hsd) throws DataAccessException {
		this.getHibernateTemplate().save(hsd);
	}

	public void delete(HomeSiteAdBannerDescription hsd) throws DataAccessException {
		this.getHibernateTemplate().delete(hsd);
	}

	public List loadByNewsClassId(Long newsClassId) throws DataAccessException {
		List list = new ArrayList();
		String sql = "";
		if(newsClassId != null){
			sql = " from HomeSiteAdBannerDescription h where 1 = 1 and h.homeSiteNewsClass.id = ?";
			list = this.getHibernateTemplate().find(sql,newsClassId);
		}else{
			sql = " from HomeSiteAdBannerDescription h where 1 = 1 ";
			list = this.getHibernateTemplate().find(sql);
		}
		
		return list;
	}

	public HomeSiteAdBannerDescription loadByClassIdAndName(Long classId, String name) throws DataAccessException {
		HomeSiteAdBannerDescription h = null;
		String sql = " from HomeSiteAdBannerDescription h where h.homeSiteNewsClass.id = ? and h.picName like ?";
		List list = this.getHibernateTemplate().find(sql,new Object[]{classId,name});
		if(StringUtils.isNotNull(list)){
			h = (HomeSiteAdBannerDescription) list.get(0);
		}
		return h;
	}

	
	public HomeSiteAdBannerDescription loadHsAdBannerById(Integer id) throws DataAccessException {
		HomeSiteAdBannerDescription hs = new HomeSiteAdBannerDescription();
		Object obj  = null;
		obj = this.getHibernateTemplate().get(HomeSiteAdBannerDescription.class, id);
		if(obj != null){
			hs = (HomeSiteAdBannerDescription)obj;
			return hs;
		}else{
			return null;
		}
	}

	public void update(HomeSiteAdBannerDescription hsd) throws DataAccessException {
		this.getHibernateTemplate().saveOrUpdate(hsd);
	}

	/* 根据 所属分类、图片描述*/ 
	public PageResultSet loadPageList(Long newsClassId,String description,Integer page, Integer pageSize) throws DataAccessException {

		PageResultSet pageResultSet = new PageResultSet();
	    PerfectPageResultSet pageResult = new PerfectPageResultSet();
	    // 获取Session
	    Session session = SessionFactoryUtils.getSession(getSessionFactory(),true);

	    // 初始化SQL语句
	    StringBuffer stringBuffer = new StringBuffer(
	            " from HomeSiteAdBannerDescription as hs  where 1=1 ");
	    
	    // 设置每页显示得记录数和开始页
	    pageResult.setPage(page, pageSize);

	    // 参数
	    List<Comparable> list = new ArrayList<Comparable>();
	    
	    // 所属类别、广告
	    if(newsClassId!=null && !newsClassId.equals(new Long(-1)))
	    {
	        stringBuffer.append(" and hs.homeSiteNewsClass.id = ?");
	        
	        list.add(newsClassId);
	    }

	    
	    //描述
	    if(description!=null && !description.equals(""))
	    {
	    	description = "%"+description+"%";
	    	
	        stringBuffer.append(" and hs.picInfo like ?");
	        
	        list.add(description);
	    }
	   
	    stringBuffer.append(" order by id desc ");

	    // 把集合转换成数组
	    String[] strParams = Tool.toArray(list);
	    
	    pageResultSet = pageResult.pageResultSet(stringBuffer.toString(),session, strParams);
	    
	    return pageResultSet;
	}

	
}
